package mrusanov.ui.newgame

import javax.swing._
import mrusanov.nation.{World, NationsLoader, Nation}
import mrusanov.localization.Localization
import java.awt.event.{ActionEvent, ActionListener}
import mrusanov.team.Club
import net.miginfocom.swing.MigLayout
import mrusanov.ui.{PanelBuilder, CentralPanel}
import mrusanov.util.swing.{Event, EventConsumer}

class NewGamePanel(consumer:EventConsumer[World]) extends CentralPanel {
  setLayout(new MigLayout("fill"))
  private val world = World.generateWorld
  private val possibleNations:Vector[Nation] = NationsLoader.nations.sortBy(_.name)

  private val nationsComboBoxModel = new DefaultComboBoxModel()
  possibleNations foreach(nationsComboBoxModel.addElement(_))

  private val selectNationLabel = new JLabel(Localization("newGame.selectNation"))
  private val selectNationComboBox = new JComboBox(nationsComboBoxModel)

  private var selectedNation:Option[Nation] = None
  private var clubsOfSelectedNation:Option[List[Club]] = None

  private val selectClubLabel = new JLabel(Localization("newGame.selectClub"))
  private val selectedClubComboBox = new JComboBox()

  selectNationComboBox.setSelectedIndex(-1)

  private val clubInfo = new JTextArea()
  clubInfo.setEditable(false)

  add(selectNationLabel, "span 1")
  add(selectNationComboBox, "span 2, wrap")
  add(selectClubLabel, "span 1")
  add(selectedClubComboBox, "span 2, wrap")
  add(clubInfo, "span 3, grow, push, wrap")
  private val newGameButton = new JButton(Localization("menu.newGame"))
  add(newGameButton, "span 3, align center")

  newGameButton.addActionListener(new ActionListener {
    def actionPerformed(e: ActionEvent) {
      val selectedClub = Option(selectedClubComboBox.getSelectedItem.asInstanceOf[Club])
      selectedClub match {
        case None => {
          JOptionPane.showMessageDialog(NewGamePanel.this, Localization("newGamePanel.pleaseSelectClub"),
            Localization("newGamePanel.pleaseSelectClub.title"), JOptionPane.WARNING_MESSAGE)
        }
        case Some(club) => {
          world.userClub = club
          consumer.consumeEvent(Event(world))
        }
      }
    }
  })

  selectNationComboBox.addActionListener(new ActionListener {
    def actionPerformed(e: ActionEvent) {
      selectedNation = Option(selectNationComboBox.getSelectedItem.asInstanceOf[Nation])
      clubsOfSelectedNation = selectedNation.map(_.clubs)

      val selectedClubModel = new DefaultComboBoxModel()
      clubsOfSelectedNation foreach( _.foreach(selectedClubModel.addElement(_)))

      selectedClubComboBox.setModel(selectedClubModel)
      selectedClubComboBox.setSelectedIndex(-1)
    }
  })

  selectedClubComboBox.addActionListener(new ActionListener {
    def actionPerformed(e: ActionEvent) {
      updateTextArea()
    }
  })

  def updateTextArea() {
    val selectedClub = Option(selectedClubComboBox.getSelectedItem.asInstanceOf[Club])
    val text = selectedClub match {
      case None => ""
      case Some(club) => {
        val builder = StringBuilder.newBuilder
        builder.append(club.name)
        builder.append("\n")
        builder.append(club.clubBudget.money)
        builder.toString()
      }
    }

    clubInfo.setText(text)
  }

  def buildItself(panelBuilder:PanelBuilder) = panelBuilder.newGame
}
